package com.zretc.util;

import java.security.MessageDigest;

public class MD5Util {
	public static final String CHARST = "UTF-8";

	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
				'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
		try {
			byte[] btlnput = s.getBytes();
			MessageDigest mdlnst = MessageDigest.getInstance("MD5");
			mdlnst.update(btlnput);
			byte[] b = mdlnst.digest();
			int j;
			StringBuffer buffer = new StringBuffer("");
			int i = 0;
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i += 256;
				}
				if (i < 16) {
					buffer.append("0");
					buffer.append(Integer.toHexString(i));
				}
			}
			return buffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getMd5(String plainText) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			return buf.toString();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static void main(String[] args) {
		System.out.println(MD5Util.getMd5("123"));
		System.out.println(MD5Util.MD5("plkopjmn"));
		System.out.println("opopop");
		System.out.println(MD5Util.MD5("mima"));
	}
}
